package com.redoor.app.stake.auth.controller;

import com.redoor.app.stake.auth.commons.DatabaseServiceStatus;
import com.redoor.app.stake.auth.commons.StakeApplicationStatus;
import com.redoor.app.stake.auth.model.BaseUser;
import com.redoor.app.stake.auth.service.BaseUserService;
import com.redoornetwork.framework.core.response.ResponseResult;
import com.redoornetwork.framework.core.response.ResultGenerator;
import com.redoor.app.stake.auth.model.BaseMenu;
import com.redoor.app.stake.auth.model.BaseMenuCriteria;
import com.redoor.app.stake.auth.service.BaseMenuService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import com.redoor.app.stake.auth.model.serviceModel.BaseMenuServiceBean;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by wangjunlei on 2018-02-07 15:19:13.
 */
@RestController
@RequestMapping("/base/menus")
public class BaseMenuController {

    @Autowired
    private BaseMenuService baseMenuService;
    @Autowired
    private BaseUserService baseUserService;

    private final Logger logger = LoggerFactory.getLogger(BaseMenuController.class);

    /**
     * 根据uuid删除菜单
     *
     * @Author zhangYiHang
     * @Date 2018/3/8 19:44
     */
    @ApiOperation(value = "删除菜单,传值:uuid='主键'", notes = "删除菜单")
    @RequiresPermissions("sys:menu:delete")
    @PostMapping("/del")
    public ResponseResult delete(String uuid) {
        try {
            this.baseMenuService.deleteMenu(uuid);
            logger.info("删除成功");
            return ResultGenerator.generatorSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.generatorFail(StakeApplicationStatus.OTHER_EXCEPTION);
        }
    }
    @ApiOperation(value = "根据用户主键查询,传值id='主键'")
    @PostMapping("/qryByUUid")
    public ResponseResult detail(String id) {
        BaseMenu baseMenu = baseMenuService.findById(id);
        return ResultGenerator.generatorSuccess(baseMenu);
    }
    /**
     * 菜单表单表查询
     * @Author zhangYiHang
     * @Date 2018/3/14 12:30
     */
    @ApiOperation( value = "菜单单表的数据查询(jsTree用)")
    @PostMapping
    public ResponseResult list() {
        List<BaseMenu> list = this.baseMenuService.qryMenuAll();
        return ResultGenerator.generatorSuccess(list);
    }

    /**
     * 菜单列表查询
     *
     * @Author zhangYiHang
     * @Date 2018/3/8 18:48
     */
    @ApiOperation(value = "菜单列表查询", notes = "菜单列表查询")
    @GetMapping("/list")
    @RequiresPermissions("sys:menu:list")
    public ResponseResult qryMenu(BaseMenuCriteria baseMenuCriteria) {
        List<BaseMenuServiceBean> list = this.baseMenuService.qryMenu(baseMenuCriteria);
        return ResultGenerator.generatorSuccess(list);
    }

    /**
     * 新增菜单
     *
     * @Author zhangYiHang
     * @Date 2018/3/8 19:34
     */
    @ApiOperation(value = "新增菜单", notes = "新增菜单")
    @RequiresPermissions("sys:menu:insert")
    @PostMapping("/insert")
    public ResponseResult insertMenu(BaseMenuCriteria baseMenuCriteria) {
        try {
            this.baseMenuService.insertMenu(baseMenuCriteria);
            logger.info("添加菜单成功");
            return ResultGenerator.generatorSuccess(StakeApplicationStatus.SUCCESS);
        } catch (Exception e) {
            logger.error("菜单添加异常,日期:"+ new Date());
            return ResultGenerator.generatorFail(StakeApplicationStatus.OTHER_EXCEPTION);
        }
    }

    /**
     * 根据主键修改菜单
     *
     * @Author zhangYiHang
     * @Date 2018/3/8 19:47
     */
    @ApiOperation(value = "根据主键修改菜单", notes = "根据主键修改菜单")
    @RequiresPermissions("sys:menu:update")
    @PostMapping("/update")
    public ResponseResult updateMenu(BaseMenuCriteria baseMenuCriteria) {
        try {
            this.baseMenuService.updateByPrimaryKey(baseMenuCriteria);
            return ResultGenerator.generatorSuccess(StakeApplicationStatus.SUCCESS);
        } catch (Exception e) {
            logger.error("菜单修改异常,主键为{},exception with {}", baseMenuCriteria.getUuid(),e.getLocalizedMessage());
            return ResultGenerator.generatorFail(StakeApplicationStatus.OTHER_EXCEPTION);
        }
    }

    /**
     * 根据用户id查询菜单列表
     *
     * @Author zhangYiHang
     * @Date 2018/3/9 15:03
     */
    @ApiOperation(value = "根据用户uuid查询树形菜单,如果传入(uuid)为空则为当前用户的权限菜单")
    @PostMapping("/left")
    public ResponseResult qryMenuUserId(String userUuid) {
        if (userUuid == null || userUuid.trim().equals("")) {
            BaseUser user = this.baseUserService.getShiroUser(null);
            userUuid = user.getUuid();
        }
        List<BaseMenuServiceBean> list = this.baseMenuService.qryMenuUserUuid(userUuid);
        return ResultGenerator.generatorSuccess(list);
    }

    /**
     * 根据角色UUid查询菜单树形列表
     *
     * @Author zhangYiHang
     * @Date 2018/3/9 15:03
     */
    @ApiOperation(value = "菜单树形结构数据,如果角色UUID为空则查询所有", notes = "菜单树形结构数据,如果角色UUID为空则查询所有")
    @GetMapping("/tree")
    public ResponseResult qryMenuTree(String uuid) {
        List<BaseMenu> tree = this.baseMenuService.qryMenuTree(uuid);
        return ResultGenerator.generatorSuccess(tree);
    }
    /**
     * 一级菜单查询
     * @Author zhangYiHang
     * @Date 2018/3/14 11:50
     */
    @ApiOperation(value = "查询一级菜单列表")
    @PostMapping("/qryPrimaryMenu")
    public ResponseResult qryPrimaryMenu(String type) {
        List<BaseMenu> menu = this.baseMenuService.qryPrimaryMenu(type);
        return ResultGenerator.generatorSuccess(menu);
    }
    /**
     * 根据菜单名称查询数据,返回查询出数量(不包含按钮)
     * @Author zhangYiHang
     * @Date 2018/3/26 11:35
     * @Param
     */
    @ApiOperation(value = "根据菜单名称查询数据,返回查询出数量(不包含按钮)")
    @ApiImplicitParam(name = "name", value = "菜单名称", paramType = "query", dataType = "String", required = true, defaultValue = "")
    @PostMapping("/qryMenuCountByName")
    public ResponseResult qryMenuCountByName(String name){
        Integer count = this.baseMenuService.qryMenuCountByName(name);
        return ResultGenerator.generatorSuccess(count);
    }
    /**
     * 新增所有按钮信息到redis
     * @Author zhangYiHang
     * @Date 2018/4/13 10:02
     * @Param
     */
    @ApiOperation(value = "新增所有按钮信息到redis")
    @PostMapping("/insertButtnAllToRedis")
    public ResponseResult insertButtnAllToRedis(){
        this.baseMenuService.insertButtnAllToRedis();
        return ResultGenerator.generatorSuccess();
    }
}
